package com.wooophone.webapp.admin.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.wooophone.mpyeah.biz.OnLineStoreBiz;
import com.wooophone.mpyeah.entity.OnLineStore;
import com.wooophone.common.web.action.BaseAction;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;

public class OnlineStoreAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private OnLineStore onLineStore;
	private OnLineStoreBiz onLineStoreBiz;
	private String keyword;
	private List<OnLineStore> storeList;
	private String brandId;
	private String filePath;
	private String uploadContentType;
	private String uploadFileName;
	private int level;

	public String list() {
		try {
			if (brandId != null && !"".equals(brandId)) {
				Map<String, Object> parameter = new HashMap<String, Object>();
				parameter.put("brandId", brandId);
				pager.setRecordCount(onLineStoreBiz.count(parameter));
				storeList = onLineStoreBiz.find(parameter,
						(pager.getCurrPageNo() - 1) * pager.getPageSize(),
						pager.getPageSize());
				// onLineStoreBiz.list((pager.getCurrPageNo() - 1)*
				// pager.getPageSize(), pager.getPageSize());
			} else {
				pager.setRecordCount(onLineStoreBiz.count());
				storeList = onLineStoreBiz.list((pager.getCurrPageNo() - 1)
						* pager.getPageSize(), pager.getPageSize());
			}
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}

	}

	public String save() {
		try {
//			if (upload != null) {
//				String imageFileName = null;
//				imageFileName = System.currentTimeMillis()
//						+ uploadFileName.substring(uploadFileName
//								.lastIndexOf("."));
//				File imageFile = new File(ServletActionContext
//						.getServletContext().getRealPath("/upload/image")
//						+ "/" + imageFileName);
//				FileUtil.copy(upload, imageFile);
//				onLineStore.setIcon("upload/image" + "/" + imageFileName);
//			}
			onLineStore.setIcon(filePath);
			onLineStore.setBrandId(brandId);
			onLineStore.setCreator(ServletActionContext.getRequest().getUserPrincipal().getName());
			onLineStore.setStatus(1);
			try {
				onLineStoreBiz.save(onLineStore);
			} catch (Exception e) {
				e.printStackTrace();
				return error(getText("onLineStore.save.exception"));
			}
			this.sendUrl="onLineStore_list.action?brandId="+brandId;
			Map<String, Object> result = new HashMap<String, Object>();
			if (onLineStore != null && onLineStore.getId() != null) {
				if (AJAX.equals(type)) {
					result.put("status", "success");
					return ajax(result);
				} else {
					return success(getText("onLineStore.save.success"));
				}
			} else {
				if (AJAX.equals(type)) {
					result.put("status", "error");
					result.put("message", getText("onLineStore.save.error"));
					return ajax(result);
				} else {
					return error(getText("onLineStore.save.error"));
				}

			}

		} catch (Exception e) {
			// TODO: handle exception
		}

		return "";
	}

	@RequiredStringValidator(fieldName = "id", key = "noid", message = "no id")
	public String delete() {
		try {
			int r = onLineStoreBiz.delete(id);
			if (r > 0) {
				if (AJAX.equals(type)) {
					return ajax(Status.success);
				} else {
					return SUCCESS;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (AJAX.equals(type)) {
			return ajax(Status.error);
		} else {
			return ERROR;
		}
	}

	public String update() {
		OnLineStore onLineStoreOld = null;
		if (onLineStore != null && onLineStore.getId() != null
				&& !"".equals(onLineStore.getId())) {
			onLineStoreOld = onLineStoreBiz.get(onLineStore.getId());
		}
		if (brandId != null && !"".equals(brandId)) {
			onLineStoreOld.setBrandId(brandId);
		}
		if(filePath!=null&&!"".equals(filePath)){
			onLineStoreOld.setIcon(filePath);
		}
		onLineStoreOld.setName(onLineStore.getName());
		onLineStoreOld.setPadUrl(onLineStore.getPadUrl());
		onLineStoreOld.setPcUrl(onLineStore.getPcUrl());
		onLineStoreOld.setPhoneUrl(onLineStore.getPhoneUrl());
		onLineStoreOld.setDescription(onLineStore.getDescription());
		
		try {
//			if (upload != null) {
//				String imageFileName = null;
//				imageFileName = System.currentTimeMillis()
//						+ uploadFileName.substring(uploadFileName
//								.lastIndexOf("."));
//				File imageFile = new File(ServletActionContext
//						.getServletContext().getRealPath("/upload/image")
//						+ "/" + imageFileName);
//				String oldFileName = onLineStoreOld.getIcon();
//				if (oldFileName != null && !"".equals(oldFileName)) {
//					oldFileName = oldFileName.substring(oldFileName
//							.lastIndexOf("/"));
//					File fileOld = new File(ServletActionContext
//							.getServletContext().getRealPath(
//									"/upload/image")
//							+ "/" + oldFileName);
//
//					if (fileOld.exists()) {
//						fileOld.delete();
//					}
//					
//				}
//				onLineStoreOld.setIcon("upload/image" + "/"
//						+ imageFileName);
//				FileUtil.copy(upload, imageFile);
//
//			}
			int r = onLineStoreBiz.update(onLineStoreOld);
			if (r > 0) {
				return success(getText("brand.update.success"));
			} else {
				return error(getText("brand.update.error"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR_PAGE;
		}

	}

	@RequiredStringValidator(fieldName = "id", key = "noid", message = "no id")
	public String view() {
		onLineStore = onLineStoreBiz.get(id);
		return SUCCESS;
	}

	public String input() {
		return SUCCESS;

	}

	@RequiredStringValidator(fieldName = "id", key = "noid", message = "no id")
	public String edit() {
		try {
			onLineStore = onLineStoreBiz.get(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String search() {
		try {
			if (null == keyword && "".equals(keyword)) {
				return list();
			} else {
				String keywordString = keyword;
				pager.setRecordCount(onLineStoreBiz.count(keywordString));
				storeList = onLineStoreBiz.search(keywordString,
						(pager.getCurrPageNo() - 1) * pager.getPageSize(),
						pager.getPageSize());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;

	}

	@RequiredStringValidator(fieldName = "id", key = "noid", message = "no id")
	public String commend() {
		try {
			onLineStore = onLineStoreBiz.get(id);
			onLineStore.setLevel(level);
			onLineStoreBiz.update(onLineStore);
			return ajax(Status.success);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return ajax(Status.error);
	}

	public String deleteSelect() {
		// try {
		// int r = 0;
		// for (int i = 0; i < .length; i++) {
		// r = brandBiz.delete(brandIdList[i]);
		// }
		// if (r > 0) {
		// if (AJAX.equals(type)) {
		// Map<String, Object> result = new HashMap<String, Object>();
		// result.put("status", "success");
		// result.put("count", r);
		// return ajax(result);
		// } else {
		// return SUCCESS;
		// }
		// }
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		// if (AJAX.equals(type)) {
		// return ajax(Status.error);
		// } else {
		// return ERROR;
		// }
		return "";
	}

	public OnLineStore getOnLineStore() {
		return onLineStore;
	}

	public void setOnLineStore(OnLineStore onLineStore) {
		this.onLineStore = onLineStore;
	}

	public List<OnLineStore> getStoreList() {
		return storeList;
	}

	public void setOnLineStoreBiz(OnLineStoreBiz onLineStoreBiz) {
		this.onLineStoreBiz = onLineStoreBiz;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public OnLineStoreBiz getOnLineStoreBiz() {
		return onLineStoreBiz;
	}

	public String getBrandId() {
		return brandId;
	}

	public void setBrandId(String brandId) {
		this.brandId = brandId;
	}

	public void setLevel(int level) {
		this.level = level;
	}

}